package com.imooc.common.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by deng_wb on 2019/8/17
 */
public class ParallelStreamAPITest {

    public static void main(String[] args) {

        List<Integer> nums = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < 10000; i++) {
            nums.add(1000000 + random.nextInt(1000000));
        }
        long start = System.currentTimeMillis();
        nums.forEach(v -> isPrime(v));
        long end = System.currentTimeMillis();
        System.out.println(end - start);

        // 使用parallel stream api

        start = System.currentTimeMillis();
        nums.parallelStream().forEach(ParallelStreamAPITest::isPrime);
        end = System.currentTimeMillis();

        System.out.println(end - start);

    }

    static boolean isPrime(int num) {
        for (int i = 2; i <= num / 2; i++) {
            if (num % i == 0)
                return false;
        }
        return true;
    }

}
